const Router = require('koa-router'),
	mongoose = require('mongoose')

let router = new Router()

router.post('/getGoodByGoodId', async ctx => {
	try {
		const Good = mongoose.model('Good'),
			goodId = ctx.request.body.goodId,
			result = await Good.findOne({ ID: goodId }).exec()

		ctx.body = {
			code: 200,
			message: result
		}
	} catch (error) {
		ctx.body = {
			code: 500,
			message: error
		}
	}
})

router.get('/getGoodListByCateId', async ctx => {
	try {
		const Good = mongoose.model('Good'),
			cateId = ctx.request.body.cateId,
			result = await Good.find({ SUB_ID: cateId }).exec()

		ctx.body = {
			code: 200,
			message: result
		}
	} catch (error) {
		ctx.body = {
			code: 500,
			message: error
		}
	}
})

router.post('/getGoodListBySubCateId', async ctx => {
	try {
		const Good = mongoose.model('Good'),
			subCateId = ctx.request.body.subCateId,
			page = ctx.request.body.page,
			num = 10,
			start = (page - 1) * num,
			result = await Good.find({ SUB_ID: subCateId })
				.skip(start)
				.limit(num)
				.exec()

        // console.log(page, subCateId)

		ctx.body = {
			code: 200,
			message: result
		}
	} catch (err) {
		ctx.body = {
			code: 500,
			message: err
		}
	}
})

module.exports = router
